﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Student
{
    public partial class InsertInfo : Form
    {
        public InsertInfo()
        {
            InitializeComponent();
        }
        //加载驱动
        public static SqlConnection sqlcon = new SqlConnection(DbHelper.constr);
        //程序加载时打开数据库
        private void Register_Load(object sender, EventArgs e)
        {
            if (sqlcon.State == ConnectionState.Closed)
            {
                sqlcon.Open();
            }
            //加载皮肤
            this.skinEngine1.Active = true;
            this.skinEngine1.SkinFile = "skin/EmeraldColor2.ssk";
        }

        //提交
        private void btn_Add_Click(object sender, EventArgs e)
        {
            //获取姓名
            string name = this.txt_Name.Text.Trim();
            //获取性别
            string sex = radioButton1.Checked ? "男" : "女";
            //获取学号
            string stuId = this.txt_StuId.Text.Trim();
            //判断学号是否存在
            string isStuId = "select StuId from StuInfo where StuId="+stuId+"";
            //调用DbHelper类中GetIDU方法
            if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(stuId) && isStuId!=stuId)
            {
                string sql = string.Format("insert into StuInfo(Name,Sex,StuId) values('{0}','{1}',{2})", name, sex, stuId);
                var result = DbHelper.GetIDU(sql);
                //显示信息
                if (result >= 1)
                {
                    MessageBox.Show("增加成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    StuInfoManagementSystem stu = new StuInfoManagementSystem();
                    stu.Show();
                }
                else
                {
                    MessageBox.Show("增加失败！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                this.Close();
            }
            else
            {
                //判断用户输入是否是null或空格
                var uName = string.IsNullOrEmpty(name);
                var uStuId = string.IsNullOrEmpty(stuId);                
                if (uName && uStuId)
                {
                    MessageBox.Show("姓名和学号不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else if (uStuId)
                {
                    MessageBox.Show("学号不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else if (uName)
                {
                    MessageBox.Show("姓名不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else if (stuId.Equals(isStuId))
                {
                    MessageBox.Show("学号已存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("出现未知错误，请联系管理员！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        //取消
        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            //关闭窗口
            this.Close();
            StuInfoManagementSystem stu = new StuInfoManagementSystem();
            stu.Show();
        }
    }
}
